import 'package:dk_flutter_base/dk_core.dart';
import 'package:dk_flutter_base/dk_ui.dart';

class DkBottomDialog<T> {
  BottomDialogOption<T> bottomSheetOption;

  DkBottomDialog.create(this.bottomSheetOption);

  DkBottomDialog show(BuildContext context) {
    var future = showDialog(
        context: context,
        builder: (BuildContext context) {
          return Material(
            color: Colors.transparent,
            child: Column(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: InkWell(
                    onTap: () {
                      if (bottomSheetOption.barrierDismissible) {
                        Navigator.of(context).pop();
                      }
                    },
                    child: Container(),
                  ),
                ),
                bottomSheetOption.widgetBuilder(context),
              ],
            ),
          );
        });
    if (null != bottomSheetOption.handleResult && null != future) {
      future.then((value) {
        bottomSheetOption.handleResult(value);
      });
    }
    return this;
  }
}

class BottomDialogOption<T> {
  HandleResult<T> handleResult;
  WidgetBuilder widgetBuilder;
  bool barrierDismissible;

  BottomDialogOption(this.widgetBuilder,
      {this.handleResult, this.barrierDismissible}) {
    this.barrierDismissible ??= true;
  }
}
